package cn.sky.demo.repository;


import cn.sky.demo.domain.entity.ReminderTask;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.time.LocalDate;
import java.util.List;

@Repository
public class ReminderTaskDao {
    
    @Autowired
    private JdbcTemplate jdbcTemplate;
    
    public void insertTask(ReminderTask task) {
        String sql = "INSERT INTO REMINDER_TASK (user_id, phone, message, start_date, next_remind_date, interval_days, is_active) VALUES (?, ?, ?, ?, ?, ?, ?)";
        jdbcTemplate.update(sql, task.getUserId(), task.getPhone(), task.getMessage(), 
                           task.getStartDate(), task.getNextRemindDate(), task.getIntervalDays(), task.getActive());
    }
    
    public List<ReminderTask> findTasksForToday() {
        String sql = "SELECT * FROM REMINDER_TASK WHERE next_remind_date <= ? AND is_active = true";
        return jdbcTemplate.query(sql, new Object[]{LocalDate.now()}, new ReminderTaskRowMapper());
    }
    
    public void updateNextRemindDate(Long taskId, LocalDate nextDate) {
        String sql = "UPDATE REMINDER_TASK SET next_remind_date = ?, updated_time = CURRENT_TIMESTAMP WHERE id = ?";
        jdbcTemplate.update(sql, nextDate, taskId);
    }
    
    public void deactivateTask(Long taskId) {
        String sql = "UPDATE REMINDER_TASK SET is_active = false, updated_time = CURRENT_TIMESTAMP WHERE id = ?";
        jdbcTemplate.update(sql, taskId);
    }

    public void recordSmsSend(Long taskId, String phone, String message, String result) {
        // 插入短信发送记录
        String sql = "INSERT INTO SMS_SEND_RECORD (task_id, phone, message, send_result) VALUES (?, ?, ?, ?)";
        jdbcTemplate.update(sql, taskId, phone, message, result);
    }

    public Integer countTargetCustomers(String receiverGroupType) {
        return 1000;
    }
}
